package com.example.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.mapper.UserMapper;
import com.example.pojo.User;

@Service
public class UserService {

	@Autowired
	private UserMapper userMapper;

	public List<User> listAllUser() {
		return userMapper.findAll();
	}

	public boolean saveUser(User user) {
		if (userMapper.exists(user.getId())) {
			return false;
		} else {
			userMapper.saveAndFlush(user);
			return true;
		}
	}

	public void deleteUser(Long id) {
		userMapper.delete(id);
	}

	public boolean updateUser(User user) {
		if (userMapper.exists(user.getId())) {
			userMapper.save(user);
			return true;
		}
		return false;
	}

	public User getUser(Long id) {
		return userMapper.getOne(id);
	}
}
